package org.ajc.auth.controller;

import org.ajc.auth.service.IRoleMenuService;
import org.ajc.auth.domain.RoleMenu;
import org.ajc.auth.query.RoleMenuQuery;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.ajc.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Autowired
    private IRoleMenuService roleMenuService;


    /**
     * 保存和修改公用的
     * @param roleMenu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody RoleMenu roleMenu){
        try {
            if( roleMenu.getId()!=null)
                roleMenuService.update(roleMenu);
            else
                roleMenuService.insert(roleMenu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            roleMenuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除多个信息
    */
    @DeleteMapping
    public AjaxResult batchDelete(List<Serializable> ids){
        try {
                roleMenuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        try {
            RoleMenu roleMenu = roleMenuService.selectById(id);
            return AjaxResult.success(roleMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< RoleMenu> list = roleMenuService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RoleMenuQuery query)
    {
        try {
            PageInfo<RoleMenu> pageInfo = roleMenuService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }
}
